<?php

require_once (dirname(__FILE__) . '/../models/UserService.php');

class PackageController extends Controller {

    public $layout = "//layouts/main-website";
//    public $layout = "//layouts/main-website-frameset";

    /**
     * get the reserved examination of one user
     * Enter description here ...
     */
    public function searchPackage() {
        $user = Yii::app()->user->getUserInfo();
        $arr = array("uid" => $user['userid']);
        $user_ser_ins = new UserService();
        $reserved_package_arr = $user_ser_ins->getMyOrder($arr);

        $this->render('reservelist', array('reserved_package_arr' => $reserved_package_arr));
    }

    public function actionYuyue() {
        $session = Yii::app()->session;
        $session['item_id'] = '1';
        $userSer = new UserService();
        if(isset($_POST["appoint"])){
            $post = $_POST;
//            $post["appoint"]["userID"] = Yii::app()->user->getId();
            $result = $userSer->appointment($post);
            if (is_int($result)) {
                JSONHelper::echoJSON(false, $result);
            }
            JSONHelper::echoJSON(TRUE);
        }
        $arr["uid"] = Yii::app()->user->getId();
        $result = $userSer->getPackageAndCenter($arr);
        
//        $userpackage = JSONHelper::retInfo(is_int($result)?false:true, $result,"");
//        var_dump(Yii::app()->user->getUserInfo());
        $this->render("yuyue", array("userpackage" => $result,"userinfo"=>Yii::app()->user->getUserInfo()));
    }

    /**
     *
     * Enter description here ...
     */
    public function actionYuyue1() {
        $user = Yii::app()->session['user_info'];
        $appoint['userID'] = $user['userid'];
        $appoint['subCenterID'] = $_POST['subCenterID'];
        $appoint['date'] = $_POST['date'];
        $arr = array("appoint" => $appoint);

        $user_ser_ins = new UserService();
        $ret = $user_ser_ins->appointment($arr);

        if ($ret === true) {
            //Yii::app()->session['user_info'] = $ret;
            $this->render('result', array("result" => "success"));
        } else {
            if ($ret == 400) {
                $message = "错误，非法请求";
            }
            if ($ret == 411) {
                $message = "提交的时间格式不正确";
            }
            if ($ret == 412) {
                $message = "预约时间不正确";
            }
            if ($ret == 413) {
                $message = "体检中心不存在";
            }
            if ($ret == 409) {
                $message = "用户不存在";
            }
            if ($ret == 416) {
                $message = "不可预约";
            }
            if ($ret == 414) {
                $message = "数据库异常";
            }
            $this->render('result', array("result" => "false", "ret" => $message));
        }
    }

    /**
     * cancel the package which you had reserved
     * get
     * Enter description here ...
     */
    public function actionPackageCancel() {
        $orderid = $_GET['orderid'];
        $user_ins = new UserService;
        $user_info = Yii::app()->user->getUserInfo();
        $uid = $user_info['userid'];
        $arr = array("uid" => $uid, "orderid" => $orderid);
        $ret = $user_ins->cancelBooking($arr);

        if ($ret == 400) {
            $message = "非法请求";
        }
        if ($ret == 425) {
            $message = "不存在订单";
        }
        if ($ret == 426) {
            $message = "非法订单号，订单和用户不一致";
        }
        if ($ret == 427) {
            $message = "不允许取消订单";
        }
        if ($ret == 414) {
            $message = "数据库异常";
        }
        if ($ret == true) {
            $message = "成功";
        }
       JSONHelper::echoJSON(true, $message);
    }

    /**
     * get the pakage of one unique person
     * Enter description here ...
     */
    public function getPackageByUid() {
        $user = Yii::app()->session['user_info'];
        $arr = array("uid" => $user['userid']);
        $user_ser_ins = new UserService();
        $package_center_arr = $user_ser_ins->getPackageAndCenter($arr);
        $this->render('yuyue', array('package_center_arr' => $package_center_arr));
    }

    /**
     * reversation frontpage
     * Enter description here ...
     */
    public function actionIndex() {
        $this->render("index");
    }

    /**
     *
     * Enter description here ...
     */
    public function actionReservedlist() {
        $this->searchPackage();
    }

    public function actionReservelist() {
        $this->getPackageByUid();
    }

}